<template>
  <div class="h-full w-full flex flex-col justify-between">
    <div class="form px-[15px] pt-[15px] border-bottom">
      <el-form>
        <el-row :gutter="12">
          <el-col :span="4">
            <el-form-item>
              <el-input v-model="form.name" placeholder="用户名/昵称/备注" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <div class="w-full flex justify-between">
                <el-date-picker
                  v-model="form.date"
                  type="daterange"
                  unlink-panels
                  range-separator="－"
                  start-placeholder="选择开始时间"
                  end-placeholder="选择结束时间"
                  :disabled-date="disableFutureDates"
                />
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="14">
            <el-form-item>
              <el-button type="primary" @click.stop="search">搜索</el-button>
              <el-button type="danger" @click.stop="handleRefresh">清空</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="grow-[1] h-0 mx-[15px] mt-[10px]">
      <el-table
        scrollbar-always-on
        ref="elTableRef"
        v-loading="state.loading"
        :data="list"
        style="width: 100%"
        height="100%"
        border
        stripe
        fit
      >
        <template #empty>
          <ui-empty />
        </template>
        <el-table-column
          align="center"
          prop="Name"
          label="用户名"
          show-overflow-tooltip
          min-width="80"
        />
        <el-table-column align="center" prop="Nickname" label="昵称" show-overflow-tooltip>
          <template #default="{ row }: TableItem">
            {{ row.Nickname || "--" }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="Note" label="备注" show-overflow-tooltip>
          <template #default="{ row }: TableItem">
            {{ row.Note || "--" }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="Coin"
          label="余额"
          show-overflow-tooltip
          min-width="120"
        >
          <template #default="{ row }: TableItem">
            <ui-amount :num="row.Coin"></ui-amount>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="BetAmount"
          label="下注金额"
          show-overflow-tooltip
          min-width="120"
        >
          <template #default="{ row }: TableItem">
            <ui-amount :num="row.BetAmount"></ui-amount>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="110">
          <template #default="{ row }: TableItem">
            <el-button type="primary" size="small" @click.stop="handleOpenDetail(row)">
              回水详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="flex justify-between my-[10px] mx-[15px]">
      <div class="flex-1 w-0 mr-[20px] flex justify-start">
        <div>
          <el-alert
            title="注意：报表最多保留两月数据。"
            type="warning"
            show-icon
            :closable="false"
          />
        </div>
      </div>
      <ui-pagination
        :total="state.total"
        v-model:pageSize="state.pageSize"
        v-model:currentPage="state.page"
        @current-change="getData"
      ></ui-pagination>
    </div>
    <!-- 详情 -->
    <rebate-detail-dialog ref="rebateDetailDialogRef" :uid="currentData?.UserId" />
  </div>
</template>
<script setup lang="ts">
import RebateDetailDialog from "./components/rebate-detail-dialog.vue";

import moment from "moment";
import { useList } from "@/hooks/useList";
import { reqReportRebate } from "@/api/report";
import { disableFutureDates, getDateArray } from "@/utils/common";

interface Params {
  StartDate: string;
  EndDate: string;
  Name?: string;
}
interface TableItem {
  row: ReportRebateMo;
}

const form = reactive({
  name: "",
  date: getDateArray()["today"]
});
const currentData = ref<ReportRebateMo | null>(null);
const rebateDetailDialogRef = ref();

const getParams = () => {
  const params: Params = {
    StartDate: moment(form.date[0]).format("YYYY-MM-DD 00:00:00"),
    EndDate: moment(form.date[1]).format("YYYY-MM-DD 23:59:59")
  };
  if (form.name) {
    params.Name = form.name;
  }
  return params;
};
const { elTableRef, state, list, getData, search } = useList<ReportRebateMo, Params>(
  reqReportRebate,
  getParams
);

// 重置
const handleRefresh = () => {
  form.name = "";
  form.date = getDateArray()["today"];
  search();
};

// 详情
const handleOpenDetail = (row: ReportRebateMo) => {
  currentData.value = row;
  nextTick(() => {
    rebateDetailDialogRef.value?.open();
  });
};

onMounted(() => {
  getData();
});
</script>
